package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.dao.AutoDao;
import io.renren.dao.MakecardvouchersdDao;
import io.renren.dao.MakecardvouchersmDao;
import io.renren.dto.MakecardvouchersmDTO;
import io.renren.entity.AutoEntity;
import io.renren.entity.MakecardvouchersdEntity;
import io.renren.entity.MakecardvouchersmEntity;
import cn.hutool.core.util.StrUtil;
import io.renren.service.MakecardvouchersmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ${comments}
 *
 * @author Sen 524227263@qq.com
 * @since 1.0.0 2024-05-30
 */
@Service
public class MakecardvouchersmServiceImpl extends CrudServiceImpl<MakecardvouchersmDao, MakecardvouchersmEntity, MakecardvouchersmDTO> implements MakecardvouchersmService {

    @Override
    public QueryWrapper<MakecardvouchersmEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<MakecardvouchersmEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Autowired
    private AutoDao autoDao;
    @Autowired
    private MakecardvouchersdDao makecardvouchersdDao;

    @Override
    public Map<String, Object> waitReceivedCoupons(String traderMOID) {
        Map<String, Object> result = new HashMap<>();
        List<AutoEntity> autoEntities = autoDao.selectList(new QueryWrapper<AutoEntity>()
                .select("AutoOID").eq("TraderM_FK", traderMOID));
        List<String> autOidArr = autoEntities.stream().map(AutoEntity::getAutooid).collect(Collectors.toList());

        QueryWrapper ew = new QueryWrapper<MakecardvouchersdEntity>();
        ew.select("MakeCardVouchersDOID");
        ew.eq("SentFlag", 1);
        if (autOidArr.size() > 0) {
            ew.in("Auto_FK", autOidArr);
        }

        List<MakecardvouchersdEntity> makecardvouchersds = makecardvouchersdDao.selectList(ew);

        result.put("num", makecardvouchersds.size());
        result.put("MakeCardVouchersMOIDArr", makecardvouchersds.stream()
                .map(MakecardvouchersdEntity::getMakecardvouchersdoid).collect(Collectors.toList()));
        return result;
    }
}